<template>
  <!-- 特殊行业、九小场所、内部单位、重点单位 -->
  <view class="myBusiness">
    <view class="content">
      <view class="content-item">
        <view class="wrapper-content">
          <scroll-view scroll-y="true" @scrolltolower="scrollBottom" :style="[
              {
                height: '100%',
              },
            ]" v-if="dataSource.length">
            <view class="wrapper-item" v-for="(v, i) in dataSource" :key="i"
              :data-url="`/guideInfo/myBusiness/regulationsCatalogue?title=${v.name}`" @click="handleClickRouter">
              <view class="items">
                <view class="lt">
                  <view class="line-hidden name">
                    {{ v.companyName }}
                  </view>
                  <view class="clause">
                    所属分类：{{ v.industryInformationName }}
                  </view>
                  <view class="clause" v-if="v.companyStatus == 1">
                    提交时间：{{ v.companyTime }}
                  </view>
                  <view class="clause" v-else-if="v.companyStatus == 2">
                    通过时间：{{ v.companyTime }}
                  </view>
                  <view class="clause" v-else-if="v.companyStatus == 3">
                    拒绝时间：{{ v.companyTime }}
                  </view>
                  <view class="clause" v-else>
                    退出时间：{{ v.companyTime }}
                  </view>
                </view>
                <view class="rt">
                  <image :src="
                      v.companyStatus == 1
                        ? baseImgUrl + 'self-state1.png'
                        : v.companyStatus == 2
                        ? baseImgUrl + 'self-state3.png'
                        : v.companyStatus == 3
                        ? baseImgUrl + 'self-state2.png'
                        : baseImgUrl + 'self-state4.png'
                    " mode=""></image>
                </view>
              </view>

              <view class="down">
                <!-- companyStatus     1.审核中 2.审核通过 3.审核失败 4.退出纳管 -->
                <block v-if="v.companyStatus != 1">
                  <view class="rt" @click="reAmendHandle(v)" v-if="v.companyStatus != 4">
                    <image :src="
                        v.companyStatus == 3
                          ? baseImgUrl + 'business2.png'
                          : baseImgUrl + 'business4.png'
                      " mode=""></image>
                    {{ v.companyStatus == 3 ? "重新提交" : "修改信息" }}
                  </view>
                  <view class="rt" v-else style="width: 160rpx"></view>
                  <view class="rt" @click="deloralter(v)">
                    <image :src="
                        v.companyStatus == 3 || v.companyStatus == 4
                          ? baseImgUrl + 'business3.png'
                          : baseImgUrl + 'business5.png'
                      " mode=""></image>
                    {{
                      v.companyStatus == 3 || v.companyStatus == 4
                        ? "删除"
                        : "企业管理员"
                    }}
                  </view>
                </block>
                <block v-else>
                  <view></view>
                </block>
                <view class="rt" @click="companyDetails(v)">
                  <image :src="baseImgUrl + 'business1.png'" mode=""></image>
                  查看详情
                </view>
              </view>
            </view>
          </scroll-view>
          <view v-if="!dataSource.length">
            <u-empty mode="list" text="暂无内容！" :icon="baseImgUrl + 'no-icon2.png'">
            </u-empty>
          </view>
        </view>
      </view>
    </view>

    <view class="btns">
      <button class="submit-btn" type="primary" :data-url="`/selfInfo/myBusiness/addBusiness?parentId=${options.id}`"
        @click="handleClickRouter" icon="map" style="width: 100%">
        + 添加企业
      </button>
    </view>

    <!-- 法人验证 -->
    <u-popup v-if="legalPersonShow" :show="legalPersonShow" :round="10" mode="center" :closeOnClickOverlay="false"
      :overlayOpacity="0.3" custom-style="top: -130rpx;width: 702rpx;" @close="close">
      <view class="popupClass">
        <view class="pop-title"> <text></text>法人验证： </view>
        <view class="pop-center">
          <u-form :model="form" ref="form" label-width="280" labelPosition="’top‘" :error-type="errorType">
            <u-form-item borderBottom>
              <u--input v-model="form.mobile" border="none" :maxlength="30" readonly="readonly"></u--input>
            </u-form-item>
            <u-form-item borderBottom>
              <view class="codeForm">
                <u--input v-model="form.mobileCode" placeholder="填写验证码" :maxlength="6" border="none"></u--input>

                <u-code :startText="'获取验证码'" :endText="'获取验证码'" ref="uCode" @change="codeChange"></u-code>
                <button class="lookbtn code-btn" @tap="getCode">
                  {{ tips }}
                </button>
              </view>
            </u-form-item>
          </u-form>

          <view class="btns">
            <button class="reset-btn lt cancelBtn" type="primary" @click="resethandle">
              取消
            </button>
            <button class="submit-btn" type="primary" @click="submitHandle">
              确认
            </button>
          </view>
        </view>
      </view>
    </u-popup>
  </view>
</template>

<script>
  import scrollTopMixin from "../../utils/scroll-top-mixin";
  import {
    messagePage,
    messageDetail,
    messageSendsms,
    legalAuthenticate,
    userFindResponsible,
  } from "@/api/myself.js";
  import {
    getScrollViewTop,
    handleClickRouter,
    setScrollViewTop,
    Toast
  } from "@/utils/util.js";

  export default {
    name: "myBusiness",
    data() {
      return {
        baseImgUrl: this.$baseImgUrl,
        options: {},
        dataSource: [],
        pageBottom: false,
        legalPersonShow: false,
        pageConfig: {
          pageNo: 1,
          pageSize: 10,
          total: 0,
        },
        form: {},
        errorType: ["none"],
        codeList: {},
        tips: "",
      };
    },
    onLoad(options) {
      this.options = options;
      uni.setNavigationBarTitle({
        title: options.navTitle,
      });
      this.getBusiness()
      uni.$on('actual_enterprises_addBusiness', () => {
        this.getBusiness()
      })
    },
    onUnload() {
      uni.$off('actual_enterprises_addBusiness')
    },
    methods: {
      getBusiness() {
        this.dataSource = [];
        this.pageConfig.pageNo = 1;
        this.getList();
      },
      handleClickRouter,
      codeChange(text) {
        this.tips = text;
      },
      getCode() {
        if (this.$refs.uCode.canGetCode) {
          // 模拟向后端请求验证码
          uni.showLoading({
            title: "正在获取验证码",
          });
          setTimeout(() => {
            uni.hideLoading();
            // 这里此提示会被this.start()方法中的提示覆盖
            uni.$u.toast("验证码已发送");
            // 通知验证码组件内部开始倒计时
            this.$refs.uCode.start();

            let data = {
              companyId: this.codeList.companyInfo.id,
              mobile: this.codeList.legalInfo.mobile,
              smsType: "UPDATE_OLD_LEGAL_MOBILE",
            };

            messageSendsms(data).then((res) => {
              if (res.code != 200) {
                Toast(res.message);
                this.legalPersonShow = false;
              }
            });
          }, 2000);
        }
      },
      resethandle() {
        this.form = {};
        this.legalPersonShow = false;
      },
      submitHandle() {
        if (!this.form.mobileCode) {
          Toast("请输入验证码");
          return false;
        }

        legalAuthenticate({
          id: this.codeList.companyInfo.id,
          mobileCode: this.form.mobileCode,
        }).then((res) => {
          if (res.code == 200) {
            let data = {
              ...this.codeList,
              oldLegalKey: res.result.authenticate,
            };
            uni.navigateTo({
              url: `/selfInfo/amendBusiness/amendBusiness?data=${encodeURIComponent(
              JSON.stringify(data)
            )}`,
            });

            this.resethandle();
          } else if (res.code == 400) {
            Toast(res.message);
          }
        });
      },
      reAmendHandle(item) {
        if (item.companyStatus == 1) {
          uni.navigateTo({
            url: "/selfInfo/myBusiness/addBusiness",
          });
        } else if (item.companyStatus == 2) {
          // 重要不要删    这是第二版的流程
          // messageDetail({ companyId: item.id }).then((res) => {
          //   if (res.code == 200) {
          //     this.codeList = { lng: item.lng, lat: item.lat, ...res.result };
          //     uni.navigateTo({
          //       url: `/selfInfo/amendBusiness/amendBusiness?data=${encodeURIComponent(
          //         JSON.stringify(this.codeList)
          //       )}`,
          //     });
          //   }
          // });

          uni.navigateTo({
            url: `/selfInfo/amendBusiness/revampSiteInfo?data=${encodeURIComponent(
            JSON.stringify(item)
          )}`,
          });

          // 重要不要删    后面要放开   第一版的流程
          // this.legalPersonShow = true;
          // messageDetail({ companyId: item.id }).then((res) => {
          //   if (res.code == 200) {
          //     this.codeList = { lng: item.lng, lat: item.lat, ...res.result };
          //     this.form.mobile = res.result.legalInfo.mobile;
          //   }
          // });
        } else if (item.companyStatus == 3) {
          uni.navigateTo({
            url: `/selfInfo/myBusiness/addBusiness?data=${encodeURIComponent(
            JSON.stringify({ ...item, backIndex: 1, parentId: this.options.id })
          )}`,
          });
        }
      },
      deloralter(v) {
        if (v.companyStatus == 2) {
          userFindResponsible({
            companyId: v.id
          }).then((res) => {
            if (res.code == 200 && res.result.responsible) {
              uni.navigateTo({
                url: `/selfInfo/modificationSite/siteInfoPerson?id=${v.id}`,
              });
            } else {
              Toast("您当前不是管理员");
            }
          });
          return;
        }

        // 删除
        uni.navigateTo({
          url: `/selfInfo/deleteBusiness/deleteBusiness?id=${v.id}&backIndex=1`,
        });
      },
      companyDetails(v) {
        // 通过的页面
        if (v.companyStatus == 2) {
          uni.navigateTo({
            url: `/selfInfo/myBusiness/passBusinessDetails?id=${v.id}&industryInformationName=${v.industryInformationName}`,
          });
          return;
        }
        // 没有通过时候的详情
        uni.navigateTo({
          url: `/selfInfo/myBusiness/businessDetails?id=${v.id}&industryInformationName=${v.industryInformationName}&source=enterprise`,
        });
      },
      scrollBottom() {
        let current = (this.pageConfig.pageNo += 1);
        let pages = Math.ceil(this.pageConfig.total / this.pageConfig.pageSize);
        if (current <= pages) {
          this.getList();
        } else {
          this.pageBottom = true;
        }
      },
      getList() {
        let data = {
          industryInformationId: this.options.id,
          pageNo: this.pageConfig.pageNo,
          pageSize: this.pageConfig.pageSize,
        };

        messagePage(data).then((res) => {
          this.dataSource = this.dataSource.concat(res.result.records || []);
          this.pageConfig.total = res.result.total;
        });
      },
    },
  };
</script>

<style lang="scss">
  .myBusiness {
    flex: 1;
    background-color: #f9f9f9;

    .content {
      margin: 0 auto;
      margin-top: 20rpx;
      margin-bottom: 20rpx;
      border-radius: 8rpx;
      width: calc(100% - 35rpx);
      height: calc(100vh - 84px);
      overflow-y: auto;

      .content-item {
        background: #fff;
      }
    }

    .wrapper-content {
      height: calc(100vh - 84px);

      .wrapper-item {
        padding: 30rpx;
        background: #ffffff;
        margin-bottom: 20rpx;

        .items {
          display: flex;

          .lt {
            width: 425rpx;
            max-width: 425rpx;

            .name {
              font-size: 30rpx;
              font-family: PingFang SC;
              font-weight: bold;
              margin-bottom: 17rpx;
              margin-top: 20rpx;
            }

            .clause {
              margin-bottom: 17rpx;
              font-size: 26rpx;
              font-family: PingFang SC;
              font-weight: 400;
              color: #a1a1a1;
            }
          }

          .rt {
            width: 192rpx;
            height: 184rpx;
            margin-left: 30rpx;

            image {
              width: 100%;
              height: 100%;
            }
          }
        }

        .down {
          display: flex;
          justify-content: space-between;
          margin-top: 20rpx;
          padding-top: 20rpx;
          border-top: 1px solid #dedcdc;
          text-align: right;

          .rt {
            font-size: 30rpx;
            font-family: PingFang SC;
            font-weight: 500;
            color: #333333;

            image {
              margin-right: 5px;
              margin-bottom: -3rpx;
              width: 29rpx;
              height: 32rpx;
            }
          }
        }
      }
    }

    .popupClass {
      .pop-title {
        padding: 30rpx 35rpx;
        border-radius: 16rpx 16rpx 0 0;

        color: #333333;
        font-size: 32rpx;
        font-weight: bold;
        background: #e9f4ff;

        text {
          margin-right: 10rpx;
          width: 20rpx;
          border-left: 4px solid #2488f5;
        }
      }

      .pop-center {
        padding: 30rpx 40rpx;

        .codeForm {
          display: flex;
          justify-content: space-around;
        }
      }

      .btns {
        margin-top: 50rpx;

        .reset-btn,
        .submit-btn {
          height: 80rpx;
          line-height: 80rpx;
        }
      }
    }
  }
</style>